package fc.sj.study.RxjavaStudy;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import fc.sj.study.HttpUtil.HttpHelper;
import fc.sj.study.HttpUtil.RetrofitProvider;
import fc.sj.study.R;
import fc.sj.study.entity.LoginRequest;
import fc.sj.study.entity.LoginResponse;
import fc.sj.study.utils.LogUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static io.reactivex.Observable.create;

public class RxJavaMainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_java_main);
        initRxjavatest2();
        retrofitTest1();
    }

    private void retrofitTest1(){
        HttpHelper httpHelper= RetrofitProvider.post().create(HttpHelper.class);
        httpHelper.login(new LoginRequest())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LoginResponse value) {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e("TAG","失败");
                    }

                    @Override
                    public void onComplete() {
                        LogUtil.e("TAG","成功");
                    }
                });
    }

    private void initRxjavatest2(){
        Observable<String> observable = create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                LogUtil.e("TAG",Thread.currentThread().getName());
                e.onNext("1");
            }
        });
        Consumer<String> consumer=new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                LogUtil.e("TAG",Thread.currentThread().getName());
                LogUtil.e("TAG",s);
            }
        };

        observable.subscribeOn(Schedulers.newThread())//开启线程执行耗时操作
                .observeOn(AndroidSchedulers.mainThread())//接收到数据后在主线程进行操作
                .observeOn(Schedulers.io())
                .subscribe(consumer);
    }

    private void initRxJavatest3(){
        create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> e) throws Exception {
                e.onNext(1);
            }
        }).map(new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) throws Exception {
                return integer.intValue()+1+"";
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String value) {
                LogUtil.e("TAG",value);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initRxjavatest1(){
        //Rxjava链式操作
        create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("第一个");
                e.onNext("第二个");
                e.onNext("第三个");
                e.onNext("第四个");
                e.onComplete();
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                LogUtil.e("TAG","onSubscribe");
            }

            @Override
            public void onNext(String value) {
                LogUtil.e("TAG",value+"");
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.e("TAG",e+"");
            }

            @Override
            public void onComplete() {
                LogUtil.e("TAG","onComplete");
            }
        });
    }

}
